package com.LeetCode.List;

import org.junit.Test;

/**
 * 合并两个有序链表
 */
public class MegreTwoList {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(-1);
        ListNode head = dummy;
        while(l1 !=null && l2 !=null){
            if(l1.val < l2.val){
                dummy.next = l1;
                dummy = dummy.next;
                l1 = l1.next;
            }else{
                dummy.next = l2;
                dummy = dummy.next;
                l2 = l2.next;
            }
        }

        dummy.next = l1 == null ? l2 : l1;
        return head.next;
    }

    @Test
    public void test(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(6);
        node1.next = node3; node3.next = node5;
        node2.next = node4; node4.next = node6;
        ListNode.printList(mergeTwoLists(node1,node2));
    }
}
